package com.jwlifer.service.couchbase;
import java.io.IOException;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;

import com.couchbase.client.CouchbaseClient;
public class CouchbaseConnector {
	
	private String servers;
	
	private String username;
	
	private String passwd;
	
	private CouchbaseClient client;
	
	public void init(){
		String[] svrs = servers.split(",");
		List<URI> uris = new LinkedList<URI>();
		for(String svr: svrs){
			svr = "http://" + svr +"/pools";
			uris.add(URI.create(svr));
		}
		try {
			this.client = new CouchbaseClient(uris,username,passwd);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void set(String key, int exp, Object o){
		client.set(key, exp, o);
	}
	
	public Object get(String key){
		return client.get(key);
	}
	
	public void close(){  
		client.shutdown();
	}

	public String getServers() {
		return servers;
	}

	public void setServers(String servers) {
		this.servers = servers;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public CouchbaseClient getClient() {
		return client;
	}

	public void setClient(CouchbaseClient client) {
		this.client = client;
	} 
	
}
